package com.wzcl.app.service.common.activity;

import com.wzcl.app.dao.model.common.Promotion;
import com.wzcl.app.model.common.MyPage;
import com.wzcl.app.model.common.WZCL;
import com.wzcl.app.model.dto.PromotionDto;
import com.wzcl.app.service.BaseService;
import org.springframework.data.jpa.domain.Specification;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

//活动的公共接口
public interface ActivityPromotion<T extends Promotion> extends BaseService<T, PromotionDto> {

    //创建优惠
    T create(PromotionDto promotionDto);

    //修改优惠
    T update(PromotionDto promotionDto);

    //修改优惠
    T getById(Integer promotionId);

    //删除优惠
    T deleteById(Integer promotionId);

    //删除活动下所有的优惠
    void deleteAllByActivityId(Integer activityId);

    //校验参数
    void checkValid(PromotionDto promotionDto);

    //与其他优惠的冲突校验
    void checkConflict(List<T> promotionList);

    //获取下面所有的优惠(orderNo 降序)
    List<T> findAllByActivityId(Integer activityId);

    MyPage<T> findPage(PromotionDto promotionDto);

    Specification<T> getSpecification(PromotionDto promotionDto);

    //该类活动的计算规则
    BigDecimal computePrice(BigDecimal currentPrice, List<T> promotions, Map<String, Object> condition);

    //计算当前活动下所有的活动优惠
    default BigDecimal computePrice(Integer activityId, BigDecimal currentPrice, Map<String, Object> condition) {
        //计算值不大于0 直接返回0
        if (!(BigDecimal.ZERO.compareTo(currentPrice) < 0)) {
            return BigDecimal.ZERO;
        }
        //先获所有的优惠内容
        List<T> promotions = findAllByActivityId(activityId);
        if (promotions != null && promotions.size() > 0) {
            BigDecimal value = computePrice(currentPrice, promotions ,condition);
            return BigDecimal.ZERO.compareTo(value) < 0?value:BigDecimal.ZERO;
        }
        return currentPrice;
    }

    //运算
    default BigDecimal compute(BigDecimal data, Integer operator, BigDecimal value) {
        if (WZCL.OPERATOR_RULE.ADD.equals(operator)) {
            return data.add(value);
        } else if (WZCL.OPERATOR_RULE.SUBTRACT.equals(operator)) {
            return data.subtract(value);
        } else if (WZCL.OPERATOR_RULE.MULTIPLY.equals(operator)) {
            return data.multiply(value);
        } else if (BigDecimal.ZERO.compareTo(value) < 0 && WZCL.OPERATOR_RULE.DIVIDE.equals(operator)) {
            return data.divide(value, 2);
        }
        return data;
    }
}
